
#include <string.h>
#include <gtk/gtk.h>


#include <v4li/v4li.h>

#include <util/mem.h>

#include "camcal.h"
#include "camcal_events.h"
static GtkFrame * __build_calibre_frame(CamCal * cc) {

	GtkWidget * frame = gtk_frame_new("Calibration Options");

	GtkWidget * vbox;
	GtkWidget * hbox;
	GtkWidget * label;

	// principal vbox
	vbox = gtk_vbox_new(FALSE, 4);
	gtk_container_add(GTK_CONTAINER(frame), vbox);

	hbox = gtk_hbox_new(FALSE, 4);
	gtk_box_pack_start_defaults(GTK_BOX(vbox), GTK_WIDGET(hbox));

	label = gtk_label_new("X:");
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(label));

	cc->spb_vertex_nx = GTK_SPIN_BUTTON(gtk_spin_button_new_with_range(4, 400, 1));
	gtk_spin_button_set_value(cc->spb_vertex_nx, 11);
	gtk_widget_set_size_request(GTK_WIDGET(cc->spb_vertex_nx), 70, -1);
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(cc->spb_vertex_nx));

	label = gtk_label_new("Y:");
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(label));

	cc->spb_vertex_ny = GTK_SPIN_BUTTON(gtk_spin_button_new_with_range(4, 400, 1));
	gtk_spin_button_set_value(cc->spb_vertex_ny, 7);
	gtk_widget_set_size_request(GTK_WIDGET(cc->spb_vertex_ny), 70, -1);
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(cc->spb_vertex_ny));

	label = gtk_label_new("Square(mm):");
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(label));

	cc->spb_square_size = GTK_SPIN_BUTTON(gtk_spin_button_new_with_range(1, 500, 1));
	gtk_spin_button_set_value(cc->spb_square_size, 25.0);
	gtk_widget_set_size_request(GTK_WIDGET(cc->spb_square_size), 70, -1);
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(cc->spb_square_size));

	hbox = gtk_hbox_new(FALSE, 4);
	gtk_box_pack_start_defaults(GTK_BOX(vbox), GTK_WIDGET(hbox));


	label = gtk_label_new("Step:");
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(label));

	cc->spb_frame_step = GTK_SPIN_BUTTON(gtk_spin_button_new_with_range(1, 100, 1));
	gtk_spin_button_set_value(cc->spb_frame_step, 20);
	gtk_widget_set_size_request(GTK_WIDGET(cc->spb_frame_step), 70, -1);
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(cc->spb_frame_step));

	label = gtk_label_new("Frames:");
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(label));

	cc->spb_n_images = GTK_SPIN_BUTTON(gtk_spin_button_new_with_range(2, 100, 1));
	gtk_spin_button_set_value(cc->spb_n_images, 20);
	gtk_widget_set_size_request(GTK_WIDGET(cc->spb_n_images), 70, -1);
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(cc->spb_n_images));

	cc->lstore_images = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_INT, G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_POINTER);

	GtkScrolledWindow * scroll = GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(NULL, NULL));
	gtk_scrolled_window_set_policy(scroll, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_box_pack_start_defaults(GTK_BOX(vbox), GTK_WIDGET(scroll));

	cc->treeview = GTK_TREE_VIEW(gtk_tree_view_new_with_model(GTK_TREE_MODEL(cc->lstore_images)));
	gtk_widget_set_size_request(GTK_WIDGET(cc->treeview), -1, 200);

	gtk_container_add(GTK_CONTAINER(scroll), GTK_WIDGET(cc->treeview));

	GtkCellRenderer * cellrendtext;
	GtkTreeViewColumn * column;

	cellrendtext = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes("Image", cellrendtext, "text", 0, NULL);
	gtk_tree_view_column_set_min_width(column, 200);
	gtk_tree_view_append_column(cc->treeview, column);

	cellrendtext = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes("N. points", cellrendtext, "text", 1, NULL);
	gtk_tree_view_append_column(cc->treeview, column);

/*
	GtkTreeIter iter;
	gtk_list_store_append(cc->lstore_images, &iter);
	gtk_list_store_set(cc->lstore_images, &iter, 0, "Ok", 1, 10, -1);
	gtk_list_store_append(cc->lstore_images, &iter);
	gtk_list_store_set(cc->lstore_images, &iter, 0, "Ok", 1, 20, -1);
	gtk_list_store_append(cc->lstore_images, &iter);
	gtk_list_store_set(cc->lstore_images, &iter, 0, "Ok", 1, 30, -1);
	gtk_list_store_append(cc->lstore_images, &iter);
	gtk_list_store_set(cc->lstore_images, &iter, 0, "Ok", 1, 40, -1);
*/

	hbox = gtk_hbox_new(FALSE, 25);
	gtk_box_pack_start_defaults(GTK_BOX(vbox), GTK_WIDGET(hbox));

	cc->btg_capture = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Capture"));
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(cc->btg_capture));
	gtk_widget_set_sensitive( GTK_WIDGET(cc->btg_capture), FALSE );

	cc->bt_clear = GTK_BUTTON(gtk_button_new_with_label("Clear All"));
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(cc->bt_clear));

/*
	cc->bt_delete = GTK_BUTTON(gtk_button_new_with_label("Delete"));
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(cc->bt_delete));
*/

	cc->bt_calibre = GTK_BUTTON(gtk_button_new_with_label("Calibre"));
	gtk_box_pack_start_defaults(GTK_BOX(hbox), GTK_WIDGET(cc->bt_calibre));

	return GTK_FRAME(frame);
}
static void __build(CamCal * cc) {

	GtkHBox * HBOX;
	GtkVBox * vbox;
	GtkFrame * frame;

	HBOX = GTK_HBOX(gtk_hbox_new(FALSE, 2));

	frame = GTK_FRAME(gtk_frame_new("Camera"));
	gtk_box_pack_start(GTK_BOX(HBOX), GTK_WIDGET(frame), TRUE, TRUE, 0);

	cc->da_image = GTK_DRAWING_AREA(gtk_drawing_area_new());
	gtk_widget_add_events(GTK_WIDGET(cc->da_image), GDK_ALL_EVENTS_MASK);
	gtk_container_add(GTK_CONTAINER(frame), GTK_WIDGET(cc->da_image));
	gtk_widget_set_size_request(GTK_WIDGET(cc->da_image), 320, 240);

	vbox = GTK_VBOX(gtk_vbox_new(FALSE, 2));
	gtk_box_pack_end(GTK_BOX(HBOX), GTK_WIDGET(vbox), TRUE, TRUE, 0);

	cc->v4li = V4L_INTERFACE(v4l_interface_new());
	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(cc->v4li), FALSE, TRUE, 0);

	frame = __build_calibre_frame(cc);
	gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(frame), FALSE, TRUE, 0);

	cc->box = GTK_HBOX( HBOX );

}
CamCal * cc_new(void) {
	CamCal * cc;

	cc = mem_alloc(CamCal);
	
	cc->ct_image_count = 0;
	cc->ct_image_count_name = 0;

	__build(cc);
	__camcal_init_events(cc);

	return cc;
}


